package com.spa.application.service.shop;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.spa.application.dto.common.rule.RuleDto;
import com.spa.application.dto.login.LoginUser;
import com.spa.application.dto.shop.MineDTO;
import com.spa.domain.entity.Rule;
import com.spa.domain.entity.Shop;
import com.spa.domain.entity.ShopUser;
import com.spa.domain.service.RuleService;
import com.spa.domain.service.ShopService;
import com.spa.domain.service.ShopUserService;
import com.spa.infrastructure.common.KeyConstant;
import com.spa.infrastructure.enums.dict.rule.RuleClassifyEnum;
import com.spa.infrastructure.exception.SpaException;
import com.spa.infrastructure.repository.condition.RuleCondition;
import com.spa.infrastructure.repository.service.SysConfigurationRepositoryService;
import com.spa.infrastructure.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ShopMineService {

    @Autowired
    private ShopService shopService;

    @Autowired
    private ShopUserService shopUserService;

    @Autowired
    private RuleService ruleService;

    @Autowired
    private SysConfigurationRepositoryService sysConfigurationRepositoryService;

    public MineDTO index() {
        LoginUser loginUser = SessionUtil.getUser();
        Shop shop = shopService.getShopById(loginUser.getShopId());
        if (BeanUtil.isNotEmpty(shop)) {
            ShopUser shopUser = shopUserService.getShopUserById(loginUser.getUserId());
            MineDTO mineDTO = new MineDTO();
            mineDTO.setAddress(shop.getAddress());
            mineDTO.setPhone(shop.getPhone());
            mineDTO.setImg(shop.getLogo());
            mineDTO.setName(shop.getName());
            mineDTO.setIdentity("10");
            mineDTO.setFlag(shop.getFlag());
            if (BeanUtil.isNotEmpty(shopUser)) {
                mineDTO.setPrincipal(0 == shopUser.getIsPrincipal() ? true : false);
            } else {
                mineDTO.setPrincipal(false);
            }
            List<String> values = sysConfigurationRepositoryService.getValueByKey(KeyConstant.SHOPUSER_WHITELIST);
            mineDTO.setIsShow(values.contains(String.valueOf(shopUser.getId())) ? 1 : 0);
            return mineDTO;
        }

        return null;
    }

    public List<RuleDto> queryShopRule(){
        Long shopId = SessionUtil.getUser().getShopId();
        if(ObjectUtil.isEmpty(shopId)){
            throw new SpaException("门店信息获取失败");
        }
        RuleCondition ruleCondition = new RuleCondition();
        ruleCondition.setRelevanceId(shopId);
        ruleCondition.setClassify(RuleClassifyEnum.$20.getCode());
        return Rule.listRuleToDto(ruleService.listRule(ruleCondition));
    }

    public void switchFlag(int flag) {
        Long shopId = SessionUtil.getUser().getShopId();
        shopService.switchFlag(shopId, flag);
    }

}
